package com.example.xyh.serviceImpl;

import com.example.xyh.dao.IShopDao;
import com.example.xyh.entity.Business;
import com.example.xyh.entity.Shop;
import com.example.xyh.service.BusinessService;
import com.example.xyh.service.ShopService;
import jakarta.annotation.Resource;
import lombok.extern.apachecommons.CommonsLog;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
@CommonsLog
public class ShopServiceImpl implements ShopService {
    @Resource
    IShopDao iShopDao;
    @Resource
    BusinessService businessService;
    @Autowired
    protected ModelMapper modelMapper;

    public ShopServiceImpl(IShopDao iShopDao) {
        this.iShopDao = iShopDao;
    }

    @Override
    public List<com.example.xyh.dto.ShopDto> getAllShopList() {
        List<Shop> shopList = iShopDao.findAll();
        List<com.example.xyh.dto.ShopDto> shopDTOList = new ArrayList<>();
        for (Shop shop : shopList) {
            com.example.xyh.dto.ShopDto shopDTO = modelMapper.map(shop, com.example.xyh.dto.ShopDto.class);
            shopDTOList.add(shopDTO);
        }
        return shopDTOList;
    }

    @Override
    public List<com.example.xyh.dto.ShopDto> findShopsByBusinessIdAndIsDeletedIsNull(Integer business_id) {
        List<Shop> shopList = iShopDao.findShopsByBusinessIdAndIsDeletedIsNull(business_id);
        List<com.example.xyh.dto.ShopDto> shopDTOList = new ArrayList<>();
        for (Shop shop : shopList) {
            com.example.xyh.dto.ShopDto shopDTO = modelMapper.map(shop, com.example.xyh.dto.ShopDto.class);
            shopDTOList.add(shopDTO);
        }
        return shopDTOList;
    }

    @Override
    public com.example.xyh.dto.ShopDto addShop(Integer business_id, String shop_name) {
        /*查询是否已经存在name相同的shop*/
        if (!iShopDao.existsByShopName(shop_name)) {
            /*获取商家对象*/
            Business business = businessService.findByBusinessId(business_id);
            /*如果商家存在，创建新的店铺*/
            if (business != null) {
                Shop newShop = Shop.builder()
                        .shopName(shop_name)
                        .business(business)
                        .build();
                iShopDao.save(newShop);
                return modelMapper.map(newShop, com.example.xyh.dto.ShopDto.class);
            } else {
                return null;
            }
        }
        return null;
    }

    @Override
    public com.example.xyh.dto.ShopDto deleteShop(String shop_name) {
        Shop shop = iShopDao.findByShopName(shop_name);
        if (shop != null) {
            shop.setIsDeleted(1);
            iShopDao.save(shop);
        }

        return modelMapper.map(shop, com.example.xyh.dto.ShopDto.class);
    }

    @Override
    public Shop findByShopId(Integer shop_id) {
        return iShopDao.findByShopId(shop_id);
    }


}
